var payValue = 1;
var payMoney = GetQueryString('payMoney');
var code = GetQueryString('code');
$('#pay-btn').html('微信支付'+payMoney+'元');
$('.payMoney>span').html(payMoney);
$('.payWay').click(function () {
    if($(this).attr('src')=='../phoneImg/no_pigeon.jpg'){
        $('.payWay').attr('src','../phoneImg/no_pigeon.jpg');
        $(this).attr('src','../phoneImg/goods_pigeon.jpg');
        payValue = $(this).attr('payValue');
        $('#pay-btn').html($(this).parent().siblings('.right-text').find('.way').html()+payMoney+'元');
    }
})
$(document).ready(function(){

    //判断是否是微信公众号浏览器
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        $('#pingxx_wx_pub').show();//显示微信支付
        $('#pingxx_wx_pub_select').attr("src","../phoneImg/goods_pigeon.jpg")
        payValue = $('#pingxx_wx_pub_select').attr('payValue');
        $('#pay-btn').html($('#pingxx_wx_pub_select').parent().siblings('.right-text').find('.way').html()+payMoney+'元');


        var open_id=GetQueryString('openid');
        if(open_id!=null){
            setItem("open_id",open_id);
            if( GetQueryString('code')!=null){
                pingxx_wx_pub_pay(GetQueryString('code'));
            }
        }

    } else{
        $('#aliPay').show();//显示支付宝支付
        $('#aliPay_select').attr("src","../phoneImg/goods_pigeon.jpg")
        payValue = $('#aliPay_select').attr('payValue');
        $('#pay-btn').html($('#aliPay_select').parent().siblings('.right-text').find('.way').html()+payMoney+'元');
    }


});
var isIos = 0,
    isAnd = 0;

$(function () {
    //ios
    setupWebViewJavascriptBridge(function(bridge) {
        /* Initialize your app here */
        bridge.callHandler('getDevice', function(responseData) {
            isIos = 1;
        })
    })

})
//Android
function isAndroid() {
    isAnd = 1;
}

document.getElementById('pay-btn').addEventListener('tap',function () {
    var applyPay = {};
    applyPay.token = getItem('token');
    applyPay.code = code;
    if(payValue==1){
        //微信支付
        pingxx_wx_pub_pay(code)
    }else if(payValue==2){
        if(ifApp()==0){
            zhifubao()
        }else if(ifApp()==2){
            //安卓
            if(isAnd==1){
                window.ddw.alipay(code,getItem('token'));
            }else{
                zhifubao();
            }
        }else if(ifApp()==1){
            //ios
            if(isIos==1){
                setupWebViewJavascriptBridge(function(bridge) {
                    /* Initialize your app here */
                    bridge.callHandler('alipay',applyPay, function(responseData) {
                        isIos = 1;
                        if(responseData=='success'){
                            window.location.href="pay_success.html"
                        }
                    })
                })
            }else{
                zhifubao();
            }
        }else{
            //其他
            zhifubao()
        }
    }else if(payValue==3){
        //银联支付
    }else if(payValue==4){
        //余额支付
        var btnArray = ['取消', '确定'];
        mui.prompt('', '','请输入支付密码', function(e) {
            if (e.index == 1) {
                $.ajax({
                    url: '/user/userLoginKey.json',
                    dataType: 'json',
                    data: {
                        name: getItem('phone'),
                        companyCode:getItem('companyCode'),
                    },
                    success: function (data) {
                        if (data.code != 1) {
                            mui.toast(data.msg);
                            return;
                        }
                        if(data.code==1){
                            var userKey = data.datas.userKey;
                            balancePay();
                            function balancePay() {
                                $.ajax({
                                    url:'/balance/pay.json',
                                    dataType:'json',
                                    data:{
                                        payChannelValue:5,
                                        code:code,
                                        token:getItem('token'),
                                        payPassword:calcMD5(e.value + userKey, 32),
                                        companyCode:getItem('companyCode'),
                                    },
                                    type:'post',
                                    success:function (data) {
                                        if(data.code==1){
                                            if(getItem('deliveryType')==1&&getItem('payType')==1){
                                                window.location.href="confirmSuccess.html?payMoney="+payMoney+'&code='+code;
                                            }else{
                                                window.location.href="pay_success.html"
                                            }
                                        }
                                        if(data.code==0){
                                            mui.toast(data.msg);
                                            return;
                                        }
                                        if(data.code==-1){
                                            window.location.href="login.html"
                                        }
                                    }
                                })
                            }
                        }
                    }
                })
            } else {

            }
        })
        document.querySelector('.mui-popup-input input').type='password'
    }

})

//android支付回调
function payState(res) {
    if(res==1){
        window.location.href="pay_success.html"
    }else{}
}

//支付宝支付
function zhifubao() {
    //支付宝支付
    $.ajax({
        url:'/ali/verifyPay.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            mark:8,
            code:code,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function () {
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'))
        },
        success:function (data) {
            $('.loading2').hide();
            if(data.code==1){
                window.location.href="/ali/pay.json?mark=8&code="+code+'&token='+getItem('token')
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return
            }
        }
    })
}

//微信公众号支付
function pingxx_wx_pub_pay(code){
    var open_id=GetQueryString('openid')==null?getItem("open_id"):GetQueryString('openid');
    $.ajax({
        url:'/wechat/pay.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            code:code,
            openid:open_id,
            t:new Date(),
            companyCode:getItem('companyCode'),
        },
        success:function(data){
            if(data.code==10){
                window.location.href=data.url;
                return;
            }
            if(data.code==0){
                mui.toast(data.msg)
            }
            if(data.code==1){
                if (typeof WeixinJSBridge == "undefined"){
                    if( document.addEventListener ){
                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign), false);
                    }else if (document.attachEvent){
                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign));
                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign));
                    }
                }else{
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',{
                            "appId" : data.appid,     //公众号名称，由商户传入
                            "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数
                            "nonceStr" : data.nonceStr, //随机串
                            "package" : data.packageValue,
                            "signType" : "MD5",         //微信签名方式:
                            "paySign" : data.paySign    //微信签名
                        },function(res){
                            if(res.err_msg == "get_brand_wcpay_request:ok"){
                                mui.toast("微信支付成功!");
                                window.location.href = '/m/phoneHtml/pay_success.html';//支付成功返回支付成功页面
                            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                                mui.toast("用户取消支付!");
                                window.location.href = '/m/phoneHtml/choosePayWay.html?code='+code+"&openid="+open_id+"&payMoney="+payMoney;//取消支付返回当前页面
                            }else{
                                mui.toast(res.err_msg);
                               /* window.location.href = '/m/phoneHtml/order.html?orderStateValue=1';//支付失败返回待支付页面*/
                            }
                        });
                }
            }
        }
    })
}
function onBridgeReady(appid,timeStamp,nonceStr,package,paySign){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId" : appid,     //公众号名称，由商户传入
            "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr" : nonceStr, //随机串
            "package" : package,
            "signType" : "MD5",         //微信签名方式:
            "paySign" : paySign   //微信签名
        },function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok"){
                mui.toast("微信支付成功!");
            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                alert("用户取消支付!");
            }else{
                alert("支付失败!");
            }
        });
}